package com.xdb.sqlparser.ast.statement;

import java.util.ArrayList;
import java.util.List;

import com.xdb.sqlparser.ast.SQLName;
import com.xdb.sqlparser.ast.AbstractSQLStatementImpl;
import com.xdb.sqlparser.visitor.SQLAstVisitor;
/**
 * 表示CREATE TABLE语句
 * @author woate
 *
 */
public class SQLCreateTableStatement extends AbstractSQLStatementImpl implements SQLDDLStatement {

	protected boolean ifNotExiists = false;
	/**
	 * 表类型
	 */
	protected Type type;
	/**
	 * 表名
	 */
	protected SQLExprTableSource tableSource;
	/**
	 * 表中所含的属性列
	 */
	protected List<SQLTableElement> tableElementList = new ArrayList<SQLTableElement>();

	// for postgresql
	private SQLExprTableSource inherits;

	public SQLCreateTableStatement() {

	}

	public SQLCreateTableStatement(String dbType) {
		super(dbType);
	}
	/**
	 * 表名
	 * @return
	 */
	public SQLName getName() {
		if (tableSource == null) {
			return null;
		}

		return (SQLName) tableSource.getExpr();
	}
	/**
	 * 表名
	 * @param name
	 */
	public void setName(SQLName name) {
		this.setTableSource(new SQLExprTableSource(name));
	}
	/**
	 * 获取表实例
	 * @return
	 */
	public SQLExprTableSource getTableSource() {
		return tableSource;
	}
	/**
	 * 设置表实例
	 * @param tableSource
	 */
	public void setTableSource(SQLExprTableSource tableSource) {
		if (tableSource != null) {
			tableSource.setParent(this);
		}
		this.tableSource = tableSource;
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public static enum Type {
		/**
		 * 全局临时表
		 */
		GLOBAL_TEMPORARY, 
		/**
		 * 局部临时表
		 */
		LOCAL_TEMPORARY
	}
	/**
	 * 表中所含的属性列
	 * @return
	 */
	public List<SQLTableElement> getTableElementList() {
		return tableElementList;
	}

	public boolean isIfNotExiists() {
		return ifNotExiists;
	}

	public void setIfNotExiists(boolean ifNotExiists) {
		this.ifNotExiists = ifNotExiists;
	}

	public SQLExprTableSource getInherits() {
		return inherits;
	}

	public void setInherits(SQLExprTableSource inherits) {
		if (inherits != null) {
			inherits.setParent(this);
		}
		this.inherits = inherits;
	}

	@Override
	protected void accept0(SQLAstVisitor visitor) {
		if (visitor.visit(this)) {
			this.acceptChild(visitor, tableSource);
			this.acceptChild(visitor, tableElementList);
			this.acceptChild(visitor, inherits);
		}
		visitor.endVisit(this);
	}
}
